import { createStore } from "vuex";

const store = createStore({
  state: () => ({
    counter: 123,
    name: 'sunyu',
    age: '18',
    friends: [
      { id:101,name:"cyd",age:20 },
      { id:102,name:"yzx",age:20 }
    ]
  }),
  getters: {
    doublecounter(state) {
      return state.counter * 2
    },
    totalage(state) {
      return state.friends.reduce((a,b) => {
        return a + b.age
      },0)
    },
    message(state,getters) {
      return `name:${state.name} age:${state.age} totalage:${getters.totalage}`
    },
    getFriendId(state) {
      return function(id) {
        const friend = state.friends.find(item => item.id === id)
        return friend
      }
    }
  },
  mutations: {
    changename(state) {
      state.name = "江南第一深情"
    },
    increment(state) {
      state.counter++
    }
  },
  actions: {
    incrementAction(context) {
      context.commit("increment")
    }
  }
})



export default store